    //
//  RootViewController.m
//  RssReader
//
//  Created by Wang Shuguang on 12-4-24.
//  Copyright 2012 Dawn. All rights reserved.
//

#import "RootViewController.h"
#import "ItemsViewController.h"
#import "ProgressView.h"



#define CHANNELBLOCK_STARTTAG 1101
@implementation RootViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//忽略状态栏
	self.wantsFullScreenLayout = YES;
	self.view.frame = CGRectMake(0, 0, 320, 480);
	
	//背景图片
	bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	//bgImageView.image = [UIImage noCacheImageNamed:@"bgimage.png"];
    bgImageView.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
	[self.view addSubview:bgImageView];
	[bgImageView release];
	
	//主滚动区域
	channelsView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
	channelsView.showsVerticalScrollIndicator = NO;
	channelsView.showsHorizontalScrollIndicator = NO;
	channelsView.pagingEnabled = YES;
	channelsView.rowHeight = 320;
	channelsView.delegate = self;
	channelsView.dataSource = self;
	channelsView.transform = CGAffineTransformMakeRotation(-M_PI_2);
	channelsView.frame = CGRectMake(0, 48,320,460);
	channelsView.backgroundColor = [UIColor clearColor];
	channelsView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.view addSubview:channelsView];
	[channelsView release];
    
    //工具栏
    //导航条
	UIImageView *navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 37)];
	navView.image = [UIImage noCacheImageNamed:@"navbar_background.png"];
	navView.userInteractionEnabled = YES;
	[self.view addSubview:navView];
    
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
    infoButton.frame = CGRectMake(290, 8, 20, 20);
    [infoButton addTarget:self action:@selector(showSetting:) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:infoButton];
    
    //进度条
	maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
	maskView.backgroundColor = [UIColor blackColor];
	maskView.alpha = 0.4;
    progressView = [[ProgressView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
	[window addSubview:maskView];
	[maskView release];
    [window addSubview:progressView];
    [progressView release];

    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [progressView setHidden:YES];
    [progressView setProgress:0.0];
	[maskView setHidden:YES];
    [channelsView reloadData];
	
	settingView = nil;
}

-(void) showSetting:(id)sender{
    if (!settingView) {
        UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
        settingView = [UIButton buttonWithType:UIButtonTypeCustom];
        settingView.frame = CGRectMake(0, 0, 320, 480);
        [settingView addTarget:self action:@selector(hideSetting:) forControlEvents:UIControlEventTouchUpInside];
        [window addSubview:settingView];
        
        UIImageView *settingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(107, 50, 211, 78)];
        settingImageView.image = [UIImage imageNamed:@"setting.png"];
        settingImageView.userInteractionEnabled = YES;
        [settingView addSubview:settingImageView];
        [settingImageView release];
        
        //下载
        UIButton *downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        downLoadBtn.frame = CGRectMake(6, 10, 99, 61);
        [downLoadBtn addTarget:self action:@selector(downLoadChannels:) forControlEvents:UIControlEventTouchUpInside];
        [settingImageView addSubview:downLoadBtn];
        //[[[downLoadBtn subviews] objectAtIndex:1] removeFromSuperview];
        //[[[downLoadBtn subviews] objectAtIndex:0] setCenter:CGPointMake(downLoadBtn.center.x, -10)];
        
        //设置频道
        UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        settingBtn.frame = CGRectMake(106, 10, 99, 61);
        [settingBtn addTarget:self action:@selector(setChannels:) forControlEvents:UIControlEventTouchUpInside];
        [settingImageView addSubview:settingBtn];
        //[[[settingBtn subviews] objectAtIndex:1] removeFromSuperview];
        //[[[settingBtn subviews] objectAtIndex:0] setCenter:CGPointMake(settingBtn.center.x, -10)];
    }else {
        settingView.hidden = NO;
    }
}

-(void) hideSetting:(id)sender{
    settingView.hidden = YES;
}

-(void) downLoadChannels:(id)sender{
    settingView.hidden = YES;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [progressView setPreTips:@""];
    [progressView setTips:@"0K"];
    [progressView setHidden:NO];
	[maskView setHidden:NO];
    
    Rss *rss = [Rss shareRss];
    rss.delegate = self;
    [rss cacheChannels];
}

-(void) setChannels:(id)sender{
    settingView.hidden = YES;
	SettingViewController *settingVC = [[SettingViewController alloc] init];
	settingVC.delegate = self;
	[self.navigationController pushViewController:settingVC animated:YES];
	[settingVC release];
}

#pragma mark -
#pragma mark SettingViewControllerDelegate
-(void) channelsCommitEditing{
	[channelsView reloadData];
}

#pragma mark -
#pragma mark RssDelegate
-(void) cacheWithChannelTitle:(NSString *)title{
    [progressView setPreTips:title];
}
-(void) cacheWithDataLength:(long long)dataLength{
    [progressView setTips:[NSString stringWithFormat:@"%.fK",(dataLength+0.0)/1024]];
}
-(void) cacheWithProgress:(float)progress{
    [progressView setProgress:progress];
    if (progress>=1.0) {
        [progressView setPreTips:@"离线完毕"];
        [self performSelector:@selector(hideProgress) withObject:nil afterDelay:1.5];
    }
}

-(void) hideProgress{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [progressView setHidden:YES];
	[maskView setHidden:YES];
    [UIView commitAnimations];
}

#pragma mark -
#pragma mark UITableViewDataSource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	//上舍入
    Rss *rss = [Rss shareRss];
	float totalNum = ([rss.channels count])/6.0;
	
	return ceil(totalNum);
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChannelCell"];
	if (!cell) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChannelCell"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		
		UIView *bgView = [[UIView alloc] initWithFrame:CGRectZero];
		bgView.transform = CGAffineTransformMakeRotation(M_PI_2);
		bgView.frame = CGRectMake(0,0,460,320);
		[cell addSubview:bgView];
		[bgView release];
		
		//加载频道
		for (int i = 0; i<3; i++) {
			for (int j = 0; j<2; j++) {
				ChannelBlockView *channelView = [[ChannelBlockView alloc] initWithFrame:CGRectMake(20*(j+1)+j*130, 14*i+10+i*130, 130, 130)];
				channelView.tag = CHANNELBLOCK_STARTTAG + 2*i+j;
				[bgView addSubview:channelView];
				[channelView release];
			}
		}
	}
    Rss *rss = [Rss shareRss];
	NSInteger totalNum = [rss.channels count];
	for (int i = indexPath.row*6,j=0; i<(indexPath.row+1)*6; i++,j++) {
		ChannelBlockView *channelView;
		channelView = (ChannelBlockView *)[cell viewWithTag:CHANNELBLOCK_STARTTAG + j];
		channelView.delegate = self;
		if (i<totalNum) {
			channelView.channelIndex = i;
			channelView.hidden = NO;
		}else {
			channelView.hidden = YES;
		}

	}
	
	return cell;
}

#pragma mark -
#pragma mark ChannelBlockViewDelegate
-(void) channelView:(ChannelBlockView *)channelView didSelectedWithUrl:(NSString *)url{
	ItemsViewController *itemsVC = [[ItemsViewController alloc] init];
	itemsVC.channelIndex = channelView.channelIndex;
	[self.navigationController pushViewController:itemsVC animated:YES];
	[itemsVC release];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark 内存管理
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
